<?php
session_start();
$username=$_SESSION['username'];

require 'page.class.php';

$db=new PDO("mysql:host=localhost;dbname=shenji","root","");
$db->query("set names utf8");

$result4=$db->prepare("select * from user where username='$username'");
$result4->execute();
$count4=$result4->rowCount();
if($count4==0){
	echo "<script>alert('用户不存在，请重新登录！');top.location.href='./login.php'</script>";
}else{
$result=$db->prepare("select * from user");
$result->execute();
$count=$result->rowCount();
$pagesize=10;//每页显示的条数
if($count%$pagesize){
	$page=ceil($count/$pagesize);

}else{
	$page=$count/$pagesize;
}
if(isset($_GET['currentpage'])){
	$currentpage=intval($_GET['currentpage']);
}else{
	$currentpage=1;
}
$start=($currentpage-1)*$pagesize;

$result2=$db->prepare("select * from user order by id desc limit $start,$pagesize");
$result2->execute();
}
$db=null;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/colResizable-1.6.min.js"></script>

<style>
      .tip{
		  width:600px;
		  height:280px;
	  }

</style>
<script type="text/javascript">
$(document).ready(function(){
  $(".click").click(function(){
     $(".tip").fadeIn(200);
  });
  
  $(".tiptop a").click(function(){
     $(".tip").fadeOut(200);
  });

});

</script>

</head>

<body>

	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="#">用户管理</a></li>
    <li><a href="#">用户列表</a></li>
    </ul>
    </div>
	
    <div class="rightinfo">
    
	    <div class="tools">
	    	<ul class="toolbar">
	        <li><a href="insert_user.php"><span><img src="images/t01.png" /></span>添加</a></li>
	        <li onclick="deletebatch()"><span><img src="images/t03.png"/></span>删除</li>
	        </ul>
	    </div>

	    <table class="tablelist">
	    	<thead>
	    	<tr>
	        <th style="width:1%;"><input  type="checkbox" name="checkbox" onclick="selectAll(this)"/></th>
	        <th style="width:12.5%;">用户名</th>
	        <th style="width:12.5%;">权限</th>
	        <th style="width:12.5%;">基本操作</th>
	        </tr>
	        </thead>
	        <tbody>
			<?php
			while($row=$result->fetch()){
			?>
			<tr>
	        <td><input  type="checkbox" name="son_checkbox" value="<?php echo $row['id'];?>"/></td>
	        <td><?php echo $row['username'];?></td>
	        <td><?php if($row['status']==0){echo "管理员";}else if($row['status']==1){echo "普通用户";}?></td>
	        <td><a  onclick="ck('<?php echo $row["id"];?>');" class="tablelink" href="#">修改</a> <a href="delete_user.php?id=<?php echo $row['id'];?>&username=<?php echo $row['username'] ?>" onclick="if(confirm('确认删除？')==false)return false;" class="tablelink">删除</a></td>
	        </tr> 
			<?php
		     }	
			?>
	        </tbody>
	    </table>
		
	    <div class="pagin">
	    	<?php 
	    		pagelinks();
	    	?>
	    </div>
		
	    <div class="tip">
		  	<div class="tiptop"><span>用户栏目</span><a id="refresh"></a></div>
	      	<div class="formbody">
	      	<form method="post" action="update_user.php">
	      	<ul class="forminfo">
		  	<input size="20" type="hidden" name="id" id="id">

		    <li><label>用户名</label>
			    <input name="username" id="username" type="text" onblur="user();" class="dfinput" value="<?php echo $row['username'];?>"/>
			    <i style="display:none;color:#f00;" id="yz_user">用户名由字母、数字组成;且长度在3~10个字符</i>
		    </li>
		    <li>
			    <label>密码</label><input name="password" id="password" type="password" class="dfinput" value="<?php echo $row['password'];?>" onblur="pass();"/>
			    <i style="display:none;color:#f00;" id="yz_pass">密码由字母、数字组成;且长度在6~16个字符</i>
		    </li>
		    <li><label>用户状态</label>
			    <select  name="status" id="status">
					<option value="1"  <?php echo (isset($row)&&$row['status']==1)?'selected=selected':'';?>>普通用户</option>
					<option value="0"  <?php echo (isset($row)&&$row['status']==0)?'selected=selected':'';?>>管理员</option>
				</select>
			</li>
		    <li><label>&nbsp;</label><input type="submit" name="submit" class="btn" value="确认保存"/></li>
	      	</ul>
		  	</form>
	      	</div>
	    </div>
	
    </div>
    
<script type="text/javascript">
$(function(){
  $("table").colResizable();
});





//$('.tablelist tbody tr:odd').addClass('odd');
function ck(id){
	$.post("select_user_id.php",{id:id},function(data){
		   $("#id").val(data['id']);
		   $("#username").val(data['username']);
		   $("#password").val(data['password']);
		   $("#status").val(data['status']);
		   $(".tip").fadeIn(200);
	},"json")				
}
function selectAll(objs){
    obj=document.getElementsByName('son_checkbox');
	for(var i=0;i<obj.length;i++){
		if(objs.checked){
			obj[i].checked=true;
		}else{
			obj[i].checked=false;
		}
		
	}
}

function deletebatch(){
      obj=document.getElementsByName('son_checkbox');
	  var arrayid=new Array();
	  for(var i=0;i<obj.length;i++){
		  if(obj[i].checked){
			 var value=obj[i].value;
			 arrayid.push(value);
		  }
		  
	  }
	  
	  if(arrayid.length==0){
		  alert("请选择需要删除的任务");
	  }else{
		  
		  if(confirm("确定要删除所选择的用户吗")){
		  		
			  $.post('delete_user.php',{arrayid:arrayid},function(data){
			  window.location.href="select_user.php";
		    });
			  
		  }else{
			  return false;
		  }
		  
	  }
	  
}


function user(){
var username=/^[\u4e00-\u9fa5a-zA-Z]{1}[\u4e00-\u9fa5A-Za-z0-9]{2,9}$/;
if(username.test($('#username').val())){
	$("#yz_user").hide();
}else{
	$("#yz_user").show();
}
}

function pass(){
var password=/^[a-zA-Z0-9_]{6,32}$/;
if(password.test($('#password').val())){
$("#yz_pass").hide();
}else{
$("#yz_pass").show();
}
}
$(function(){

$("#username").focus(function(){
	$("#yz_user").hide();
})
$("#password").focus(function(){
	$("#yz_pass").hide();
})
$("#refresh").click(function(){
	location.reload();
})
})
$(function(){
	$('.btn').bind('click',function(event){
		var username=/^[\u4e00-\u9fa5a-zA-Z]{1}[\u4e00-\u9fa5A-Za-z0-9]{2,9}$/;			
		if(username.test($('#username').val())){
			$("#yz_user").hide();			
		}else{
			$("#yz_user").show();				
			return false;
		}			
	})
})
	$(function(){
	$('.btn').bind('click',function(event){
		
		var password=/^[a-zA-Z0-9_]{6,32}$/;
		if(password.test($('#password').val())){				
			$("#yz_pass").hide();
		}else{				
			$("#yz_pass").show();
			return false;
		}			
	})
})
 $(document).ready(function(){
  
  var username="<?php echo $_SESSION['username']; ?>";
  if(username==null||username.length==0){		  
	 window.top.location.href="../login.php";
  }
  
})  
</script>

</body>

</html>
